#include "wt.h"

TPGen::TPGen(WT *window, QObject* parent) : QThread(parent)
{
	qsrand( QTime::currentTime().msec() );
	this->live = 1;
	this->adjustT = 0;
	this->window = window;
	this->f = fopen("/proc/temprobe","w");
	if(!this->f)
	  {
		perror("/proc/temprobe");
		exit(1);
	  }
	connect(this, SIGNAL(showtemperature()), this->window, SLOT(showtemperature()), Qt::DirectConnection);
}

void TPGen::run()
{
	while(this->live)
	  {
		if(this->adjustT)
		  {
			if(qrand()&1) // thread safe version of rand()
				this->window->temperature += 0.02;
			else
				this->window->temperature -= 0.02;
			emit showtemperature();
			int T = this->window->temperature * 0x7F;
			char buffer[8] = {0};
			buffer[0] = 1;
			buffer[1] = ::nextcountervalue();
			buffer[2] = (T>>8)&0xFF;
			buffer[3] = T&0xFF;
			fseek(f, 0, SEEK_SET);
			fwrite(buffer, 8, 1, f);
			fflush(f);
		  }
		this->msleep(this->window->getperiod());
	  }
	printf("subthread 'TPGen' teminated.\n");
}
